package com.compass.doctor.ui.patient.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutPrescribedItemBinding
import com.compass.doctor.databinding.LayoutPrescriptionRecordItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.visible
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.Drug
import com.compass.framework.model.TemplateDetails
import com.compass.framework.utils.DateUtils

/**
 * Description： 患者处理记录adapter
 * Date:2024/1/26 17:25
 */



class PatientPrescriptionRecordAdapter  constructor(private var itemType: Int) :
    BaseMultiItemAdapter<TemplateDetails>() {
    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }

    private  var onCustomBottomClick: OnCustomButtonClick?=null
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomBottomClick = onChatBottomClick
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: TemplateDetails?,
        position: Int
    ) {
        if (item == null) return
        //  1、患者详情处方记录 2、患者沟通处方记录
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutPrescriptionRecordItemBinding>()
                binding.apply {
                    tvName.text=item.doctorName
                    createTime.text=DateUtils.conversionTime(item.createTime,DateUtils.FORMAT_TWO)
                    tvDiagnosis.text=getDiagnosis(item.diagnosis)
                    tvPrescriptionDrug.text=getDrugs(item.drugs)
                    tvMedicineType.text= DictionariesManager.getOrderStatus(item.status)
                }

            }
            else -> {
                val binding = holder.binding.saveAs<LayoutPrescribedItemBinding>()
                binding.apply {
                    tvPatientName.text=item.patientName
                    tvTime.text = DateUtils.conversionTime(item.createTime,DateUtils.FORMAT_TWO)
                    tvPaymentStatus.text=DictionariesManager.getOrderStatus(item.status)
                    tvDiagnosis.text=getDiagnosis(item.diagnosis)
                    tvDrugs.text=getDrugs(item.drugs)
                    tvCancellation.onClick {
                        onCustomBottomClick?.onButtonCallBack(position, "cancellation")
                    }

                    tvDetails.onClick {
                        onCustomBottomClick?.onButtonCallBack(position, "details")

                    }

                    when(item.status){
                        "0","1"->{
                            tvCancellation.visible()
                            llReason.gone()
                        }
                        "-1"->{
                            //作废
                            tvCancellation.gone()
                            llReason.visible()
                            tvReasonName.text="作废原因"
                            tvReason.text=item.cancelDescription
                        }
                        "-2"->{
                            //拒绝
                            tvCancellation.gone()
                            tvReasonName.text="拒绝原因"
                            llReason.visible()
                            tvReason.text=item.cancelDescription
                        }
                        else->{
                            llReason.gone()
                            tvCancellation.gone()
                        }
                    }

                }
            }
        }


    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutPrescriptionRecordItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutPrescribedItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }

    private fun getDiagnosis(list:MutableList<Diagnosis>?):String{
        list?.let {
            for (item in list){
                if (item.main) return item.name
            }
        }
        return ""
    }

    private fun getDrugs(list:MutableList<Drug>?):String{
        var listName=ArrayList<String?>()
        list?.let {
            for (item in list){
                listName.add(item.name)
            }
        }
        return  listName.joinToString(separator ="/")
    }
}